using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using ServiceControllerApp.Models;

namespace ServiceControllerApp
{
    public partial class ServiceEditWindow : Window
    {
        public ObservableCollection<string> ServiceNames { get; set; }
        public bool IsSaved { get; private set; } = false;

        public ServiceEditWindow(List<string> currentServiceNames)
        {
            InitializeComponent();
            
            ServiceNames = new ObservableCollection<string>(currentServiceNames);
            lstServices.ItemsSource = ServiceNames;
            
            InitializePlaceholders();
        }

        private void InitializePlaceholders()
        {
            txtNewService.Foreground = Brushes.Gray;
            txtNewService.Text = "请输入系统服务名称";
        }

        private void TextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            var textBox = sender as TextBox;
            if (textBox != null && textBox.Foreground == Brushes.Gray && textBox.Text == textBox.Tag?.ToString())
            {
                textBox.Text = "";
                textBox.Foreground = Brushes.Black;
            }
        }

        private void TextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            var textBox = sender as TextBox;
            if (textBox != null && string.IsNullOrWhiteSpace(textBox.Text))
            {
                string? hintText = textBox.Tag?.ToString();
                textBox.Text = hintText ?? "";
                textBox.Foreground = Brushes.Gray;
            }
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            string? serviceName = txtNewService.Text?.Trim();
            
            // 检查是否为空或占位符文本
            if (string.IsNullOrEmpty(serviceName) || serviceName == txtNewService.Tag?.ToString())
            {
                MessageBox.Show(this, "请输入有效的服务名称", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            // 检查是否已存在
            if (ServiceNames.Contains(serviceName))
            {
                MessageBox.Show(this, "该服务名称已存在", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            ServiceNames.Add(serviceName);
            
            // 清空输入框并重置占位符
            txtNewService.Text = txtNewService.Tag?.ToString() ?? "请输入系统服务名称";
            txtNewService.Foreground = Brushes.Gray;
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            var button = sender as Button;
            if (button?.Tag is string serviceName)
            {
                var result = MessageBox.Show(this, 
                    $"确定要删除服务 '{serviceName}' 吗？", 
                    "确认删除", 
                    MessageBoxButton.YesNo, 
                    MessageBoxImage.Question);
                
                if (result == MessageBoxResult.Yes)
                {
                    ServiceNames.Remove(serviceName);
                }
            }
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (ServiceNames.Count == 0)
            {
                var result = MessageBox.Show(this, 
                    "服务列表为空，确定要保存吗？这将清空所有监控的服务。", 
                    "确认保存", 
                    MessageBoxButton.YesNo, 
                    MessageBoxImage.Question);
                
                if (result != MessageBoxResult.Yes)
                {
                    return;
                }
            }

            IsSaved = true;
            this.DialogResult = true;
            this.Close();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        // 获取编辑后的服务名称列表
        public List<string> GetServiceNames()
        {
            return ServiceNames.ToList();
        }
    }
} 